package co.yixiang.modules.shop.web.controller;

import co.yixiang.annotation.AnonymousAccess;
import co.yixiang.aop.log.Log;
import co.yixiang.modules.order.web.dto.InvestCacheDTO;
import co.yixiang.modules.order.web.param.InvestOrderParam;
import co.yixiang.modules.shop.entity.YxStoreProduct;
import co.yixiang.modules.shop.entity.YxSystemConfig;
import co.yixiang.modules.shop.service.YxSystemConfigService;
import co.yixiang.modules.shop.service.impl.YxInvestOrderServiceImpl;
import co.yixiang.modules.shop.service.impl.YxSystemConfigServiceImpl;
import co.yixiang.modules.shop.web.param.YxSystemConfigQueryParam;
import co.yixiang.modules.shop.web.vo.YxInvestCartQueryVo;
import co.yixiang.modules.shop.web.vo.YxStoreProductQueryVo;
import co.yixiang.modules.shop.web.vo.YxSystemConfigQueryVo;
import co.yixiang.common.web.controller.BaseController;
import co.yixiang.common.api.ApiResult;
import co.yixiang.utils.SecurityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

import co.yixiang.common.web.vo.Paging;
import co.yixiang.common.web.param.IdParam;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 配置表 前端控制器
 * </p>
 *
 * @author hisen
 * @since 2020-06-17
 */
@Slf4j
@RestController
@Api(value = "配置管理", tags = "投资端:配置管理", description = "配置管理")
public class YxSystemConfigController extends BaseController {

    @Autowired
    private YxSystemConfigService yxSystemConfigService;
    @Autowired
    private YxInvestOrderServiceImpl yxInvestOrderService;
    @Autowired
    private YxSystemConfigServiceImpl yxSystemConfigServiceImpl;




    @Log("配置管理")
    @ApiOperation(value = "配置管理", notes = "配置管理")
    @GetMapping(value = "/systemConfig/getYxSystemConfig")
    public ApiResult<String> getYxSystemConfig(String name) throws Exception{


        String data = yxSystemConfigService.getData(name);



        return ApiResult.ok(data);
}
//
    @Log("获取买卖合同")
    @ApiOperation(value = "获取买卖合同", notes = "获取买卖合同")
    @PostMapping(value = "/systemConfig/getInvestContract/{key}")
    public ApiResult<String> getInvestContract(@RequestBody @Valid InvestOrderParam param,
                                               @PathVariable String key) throws Exception{
        int uid = SecurityUtils.getUserId().intValue();

        InvestCacheDTO cacheDTO = yxInvestOrderService.getCacheOrderInfo(uid, key);
        List<YxInvestCartQueryVo> cartInfo = cacheDTO.getCartInfo();


        //investName
        //investPhone

        String data = yxSystemConfigService.getData("invest_contract");
        data = data.replace("investName",param.getInvestName()).replace("investPhone",param.getInvestPhone()).replace("nowTime",new SimpleDateFormat("yyyy年MM月dd日").format(new Date())).replace("signPicUrl","");




        return ApiResult.ok(data);
    }

    @Log("获取委托销售合同")
    @ApiOperation(value = "获取委托销售合同", notes = "获取委托销售合同")
    @PostMapping(value = "/systemConfig/getInvestSaleAgreement/{key}")
    public ApiResult<String> getInvestSaleAgreement(@RequestBody @Valid InvestOrderParam param,
                                                    @PathVariable String key) throws Exception{
        int uid = SecurityUtils.getUserId().intValue();

        InvestCacheDTO cacheDTO = yxInvestOrderService.getCacheOrderInfo(uid, key);
        List<YxInvestCartQueryVo> cartInfo = cacheDTO.getCartInfo();
        Double price = cacheDTO.getPrice();
        Integer totalNum = cartInfo.size();
        String productName ="";
        for(YxInvestCartQueryVo yxInvestCartQueryVo : cartInfo){
            YxStoreProductQueryVo yxStoreProductQueryVo = yxInvestCartQueryVo.getProductInfo();
            productName = yxStoreProductQueryVo.getStoreName()+",";

        }


        String data = yxSystemConfigService.getData("invest_sale_agreement");
        int expireMonth = Integer.parseInt(yxSystemConfigServiceImpl.getData("expire_month"));
        int hesitationDate = Integer.parseInt(yxSystemConfigServiceImpl.getData("invest_hesitation_date"));
        Calendar dayTime = Calendar.getInstance();
        dayTime.setTime(new Date());
        dayTime.add(Calendar.DAY_OF_MONTH, hesitationDate);

        Calendar monthTime = Calendar.getInstance();
        monthTime.setTime(new Date());
        monthTime.add(Calendar.MONTH, expireMonth);
        data = data.replace("investName",param.getInvestName()).replace("investPhone",param.getInvestPhone()).replace("investBankCard",param.getInvestBankCard()).
                replace("investBankName",param.getInvestBankName()).replace("nowTime",new SimpleDateFormat("yyyy年MM月dd日").format(new Date())).replace("price",String.valueOf(price))
                .replace("produtName",productName).replace("signPicUrl","").replace("newBeginTime",new SimpleDateFormat("yyyy年MM月dd日").format(dayTime.getTime())).replace("newEndTime",new SimpleDateFormat("yyyy年MM月dd日").format(monthTime.getTime()));





        return ApiResult.ok(data);
    }



}

